package com.share.finance.risk.rules.utils;

import com.alibaba.fastjson.JSON;
import com.share.finance.risk.common.model.sys.RuleNode;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class DrlUtil {
    public static String toJsonStr(Object var1) {
        if (var1 == null) {
            return null;
        } else {
            return JSON.toJSONString(var1);
        }
    }

    public static boolean isNotRun(List<String> ruleNameList, String ruleName) {
        boolean contains = ruleNameList.contains(ruleName);
        return !contains;
    }

    public static int strCompare(Object p1, Object p2) {
        String strP1 = p1.toString();
        String strP2 = p2.toString();
        return strP1.compareTo(strP2);
    }

    public static int intCompare(Object p1, Object p2) {
        String strP1 = p1.toString();
        String strP2 = p2.toString();

        Integer np1 = Integer.parseInt(strP1);
        Integer np2 = Integer.parseInt(strP2);
        return np1.compareTo(np2);
    }

    public static int doubleCompare(Object p1, Object p2) {
        String strP1 = p1.toString();
        String strP2 = p2.toString();

        Double np1 = Double.parseDouble(strP1);
        Double np2 = Double.parseDouble(strP2);
        return np1.compareTo(np2);
    }


    public static boolean addRunNode(List<RuleNode> ruleList, List<String> ruleNameList, String ruleName, Object resultType, Object creditLimit, Object nextIntIds) {
        //1.名字
        ruleNameList.add(ruleName);

        //2.节点信息
        String[] split = ruleName.split("\\|");
        String a1 = split[0];
        String a2 = split[1];
        String level = split[2];
        Integer salience = null;
        if (StringUtils.isNotBlank(level)) {
            salience = Integer.parseInt(level);
        }

        RuleNode node = new RuleNode();
        node.setExeNanoTime(System.nanoTime());//设置纳秒时间
        node.setSalience(salience);//规则的优先级
        node.setName(ruleName);
        node.setResultType(resultType == null ? null : resultType.toString());
        node.setCreditLimit(creditLimit == null ? null : creditLimit.toString());
        node.setIntIds(nextIntIds == null ? null : nextIntIds.toString());

        ruleList.add(node);
        return true;
    }
}
